<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			//改进上面计算年龄的例子，如果当前月日超过生日的月日，那么年龄+1。
			function Person(name, sex, birthday) {
				this.name = name;
				this.sex = sex;
				this.birthday = birthday;
				this.getAge = function() {
					let date = new Date();
					let dateYear = date.getFullYear();//获取当前年份
					let dateMonth = date.getMonth() + 1;//获取当前月份
					let dateDay = date.getDate();//获取当前日期

					let birth = new Date(this.birthday);
					let birthdayYear = birth.getFullYear();//获取生日的年份
					let birthMonth = birth.getMonth() + 1;//获取生日的月份
					let birthDay = birth.getDate();//获取生日的日期
					
					let age =  dateYear - birthdayYear;//当前年份减去生日年份
					
					//如果当前月份小于生日月份说明说明生日未过，岁数减一。如果月份相同，当前日期小于生日的日期也说明生日未过
					if(dateMonth < birthMonth || (dateMonth == birthMonth && dateDay < birthDay)){
						age = age - 1;
					}return age;
				}
			}
			let person = new Person("李欧雯", "女", "2000-8-30");
			console.log(person.getAge());
			console.log(person);
		</script>
	</body>
</html>
